#!/bin/bash
echo -e "Debug:\t\t File[${BASH_SOURCE[0]}]"
mkdir_directory() {
  if $DEBUG ;then
    echo -e "Debug:\t\t line[$LINENO] func[${FUNCNAME[0]}] args[$@]"
  fi
  if [ ! -d "$1" ]; then
    echo -e "Creating: $1"
    mkdir -p "$1"
  else
    if $DEBUG ;then
      echo -e "Already:\t $1 exists."
    fi
  fi
}
mkidir_directorys() {
  echo -e "${FUNCNAME[0]}:$@"
  for i in $*; do
    create_directory "$i"
  done
}

create_deb() {
  if $DEBUG ;then
    echo -e "Debug:\t\t line[$LINENO] func[${FUNCNAME[0]}] args[$@]"
  fi
  OPTIND=1
  while getopts "b:c:n:v:p:N:" opt; do
    case $opt in
      b) bin_path=$OPTARG ;;
      c) config_path=$OPTARG ;;
      n) name=$OPTARG ;;
      v) version=$OPTARG ;;
      p) path=$OPTARG ;;
      N) name_prefix=$OPTARG ;;
      \?) echo -e "Usage: $0 [-n name] [-v version] [-p install path] [-N name_prefix] [-b bin_path] [-c config_path]" ;;
    esac
  done
  if $DEBUG ;then
    echo -e "Parameter name: $name"
    echo -e "Parameter version: $version"
    echo -e "Parameter path: $path"
    echo -e "Parameter name_prefix: $name_prefix"
    echo -e "Parameter bin_path: $bin_path"
    echo -e "Parameter config_path: $config_path"
  fi
  programs_name+=($name)
  local deb_root=$path/$name
  local now_path="$deb_root/DEBIAN"
  mkdir_directory $now_path
  local control_file=${now_path}/control
  if [ ! -f "$control_file" ]; then
    echo -e "Creating: '$control_file'"
    echo -e "
Package: $name
Version: $version
Section: utils
Priority: optional
Architecture: $architecture
Maintainer: GuoSong Ji 210214599@qq.com
Description: $name" > "$control_file"
    chmod +x $control_file
  fi
#Recommends: libobject0
#Depends: libobject0
  local current_version=$(grep -i "^Version:" "$control_file" | awk '{print $2}')
  if [ "$current_version" != "$version" ]; then
      echo -e "Version mismatch: current version is '$current_version', but '$version' was provided."
      sed -i "s/^Version: .*/Version: $version/" "$control_file"
      echo -e "Updating:\t Version to '$version'."
  else
    if $DEBUG ;then
        echo -e "Already:\t Version is up-to-date."
    fi
  fi
  local file=${now_path}/postinst
  if [ ! -f "$file" ]; then
    echo -e "Creating: '$file'"
    echo -e "
#!/bin/sh
echo -e \"this is: \$0\"
set -e
# Commands to run when installing the package
TARGET_NAME=\"$name_prefix$name\"
systemctl daemon-reload
systemctl enable \$TARGET_NAME
systemctl restart \$TARGET_NAME
exit 0" > "$file"
    chmod +x $file
  else
    if $DEBUG ;then
      echo -e "Already:\t $file exists."
    fi
  fi
  local now_path="$deb_root$install_config_path/${name}"
  mkdir_directory $now_path
  local config_file=${now_path}/${name}.xml
  if [ ! -f "$config_file" ]; then
    echo -e "Creating: $config_file"
    echo -e "<dev></dev>" > "$config_file"
  else
    if $DEBUG ;then
      echo -e "Already:\t $config_file exists."
    fi
  fi
  local now_path="$deb_root/etc/systemd/system"
  mkdir_directory $now_path
  local file=${now_path}/$name_prefix${name}.service
  if [ ! -f "$file" ]; then
    echo -e "Creating: '$file'"
    echo -e "
[Unit]
Description=${name} Service
After=network-online.target
Before=nss-lookup.target
Wants=network-online.target nss-lookup.target

[Service]
ExecStart=$bin_path/${name} -c $config_path/$name/$name.xml
ExecStop=/bin/kill -SIGINT \$MAINPID
#Restart=on-failure
#RestartSec=3s
#User=imch

[Install]
WantedBy=multi-user.target
  " > "$file"
  else
    if $DEBUG ;then
      echo -e "Already:\t $file exists."
    fi
  fi
  local now_path="$deb_root$bin_path"
  mkdir_directory $now_path
}
#create_deb -p $deb_path -n config -v 2.2.1 -N $name_prefix -b $install_bin_path -c $install_config_path
create_lib_deb() {
  if $DEBUG ;then
    echo -e "Debug:\t\t line[$LINENO] func[${FUNCNAME[0]}] args[$@]"
  fi
  OPTIND=1
  while getopts "b:c:n:v:p:N:" opt; do
    case $opt in
      b) bin_path="$OPTARG" ;;
      c) config_path=$OPTARG ;;
      n) name=$OPTARG ;;
      v) version=$OPTARG ;;
      p) path=$OPTARG ;;
      N) name_prefix=$OPTARG ;;
      \?) echo -e "Usage: $0 [-n name] [-v version] [-p install path] [-N name_prefix] [-b bin_path] [-c config_path]" ;;
    esac
  done
  if $DEBUG ;then
    echo -e "Parameter name: $name"
    echo -e "Parameter version: $version"
    echo -e "Parameter path: $path"
    echo -e "Parameter name_prefix: $name_prefix"
    echo -e "Parameter bin_path: $bin_path"
    echo -e "Parameter config_path: $config_path"
    echo -e "${FUNCNAME[0]}:$@"
  fi
  local major_version="${version%%.*}"    # 去掉 "." 后的部分
  local now_name=$name$major_version
  libs_name+=($now_name)
  local deb_root=$path/$now_name
  local now_path="$deb_root/DEBIAN"
  mkdir_directory $now_path
  local control_file=${now_path}/control
  if [ ! -f "$control_file" ]; then
    echo -e "Creating: '$control_file'"
    echo -e "
Package: $name
Version: $version
Section: utils
Priority: optional
Architecture: $architecture
Maintainer: GuoSong Ji 210214599@qq.com
Description: $name" > "$control_file"
#Recommends: libobject0
    chmod +x $control_file
  fi
  local current_version=$(grep -i "^Version:" "$control_file" | awk '{print $2}')
  if [ "$current_version" != "$version" ]; then
      echo -e "Version mismatch: current version is '$current_version', but '$version' was provided."
      sed -i "s/^Version: .*/Version: $version/" "$control_file"
      echo -e "Updating:\t Version to '$version'."
  else
    if $DEBUG ;then
      echo -e "Already:\t Version is up-to-date."
    fi
  fi
  local file=${now_path}/postinst
  if [ ! -f "$file" ]; then
    echo -e "Creating: '$file'"
    echo -e "
#!/bin/sh
echo -e \"this is: \$0\"
set -e
exit 0" > "$file"
    chmod +x $file
  else
    if $DEBUG ;then
      echo -e "Already:\t $file exists."
    fi
  fi
  local now_path="$deb_root$bin_path"
  mkdir_directory $now_path
}
#create_lib_deb -p $deb_path -n libconfig -v 2.2.1 -N $name_prefix -b $install_lib_path -c $install_config_path
deb() {
  if $DEBUG ;then
    echo -e "Debug:\t\t line[$LINENO] func[${FUNCNAME[0]}] args[$@]"
  fi
  OPTIND=1
  while getopts "b:c:n:v:p:N:o:" opt; do
    case $opt in
      b) bin_path=$OPTARG ;;
      c) config_path=$OPTARG ;;
      n) name=$OPTARG ;;
      v) version=$OPTARG ;;
      p) path=$OPTARG ;;
      N) name_prefix=$OPTARG ;;
      o) other_param=$OPTARG ;;
      \?) echo -e "Usage: $0 [-n name] [-v version] [-p install path] [-N name_prefix] [-b bin_path] [-c config_path] [-o other]" ;;
    esac
  done
  if $DEBUG ;then
    echo -e "Parameter name: $name"
    echo -e "Parameter version: $version"
    echo -e "Parameter path: $path"
    echo -e "Parameter name_prefix: $name_prefix"
    echo -e "Parameter bin_path: $bin_path"
    echo -e "Parameter config_path: $config_path"
    echo -e "Parameter other_param: $other_param"
  fi
  #exit
  IFS=';' read -r -a array <<< "$other_param"
  for ((i=0; i<${#array[@]}; i+=2)); do
    if $DEBUG ;then
      echo -e "Debug:\t\t${array[i]} ${array[i+1]}"
    fi
    local name=${array[i]}
    local version=${array[i+1]}

    if [[ "$name" == lib* ]]; then
      if $DEBUG ;then
        echo -e "Debug:\t\t $name is library"
      fi
      create_lib_deb -p $deb_path -n $name -v $version -N $name_prefix -b $install_lib_path -c $install_config_path
      local extracted="${name#lib}"  # 去掉 "lib"
      local major_version="${version%%.*}"    # 去掉 "." 后的部分
      local lib_name=$name.so
      #local lib_name=$name.so.$major_version
      local path_name=$name$major_version
      local bin=$build_bin_path/$extracted/$lib_name
      local bin_new_path=$deb_path/$path_name$install_lib_path
      #if [[ "$name" != libffmpeg* ]]; then
      if [[ "$name" != libwget* ]]; then
        rm $bin_new_path/* 2>/dev/null
      fi
      cp -P $bin $bin_new_path
      cp $bin.$major_version $bin_new_path
      rm $deb_package_path/${name}_*.deb 2>/dev/null
      dpkg-deb -b -Zgzip $deb_path/$path_name $deb_package_path/${path_name}_${version}_$architecture.deb
    else
      if $DEBUG ;then
        echo -e "Debug:\t\t $name is program"
      fi
      create_deb -p $deb_path -n $name -v $version -N $name_prefix -b $install_bin_path -c $install_config_path
      local bin=$build_bin_path/$name/$name
      local bin_new_path=$deb_path/$name$install_bin_path
      rm $bin_new_path/* 2>/dev/null
      cp $bin $bin_new_path
      if [ "$name" == "config" ]; then
        local deb_root=$deb_path/$name
        cp $build_bin_path/../../$name/$name.db $deb_root$install_config_path/$name
      fi
      rm $deb_package_path/${name}_*.deb
      dpkg-deb -b -Zgzip $deb_path/$name $deb_package_path/${name}_${version}_$architecture.deb
    fi
  done
}
#deb -p $deb_path -N $name_prefix -b $install_bin_path -c $install_config_path -o "config 2.2.2 libcc 2.1.1"
deb_ts() {
  if $DEBUG ;then
    echo -e "Debug:\t\t line[$LINENO] func[${FUNCNAME[0]}] args[$@]"
  fi
  OPTIND=1
  while getopts "b:c:n:v:p:N:o:" opt; do
    case $opt in
      b) bin_path=$OPTARG ;;
      c) config_path=$OPTARG ;;
      n) name=$OPTARG ;;
      v) version=$OPTARG ;;
      p) path=$OPTARG ;;
      N) name_prefix=$OPTARG ;;
      o) other_param=$OPTARG ;;
      \?) echo -e "Usage: $0 [-n name] [-v version] [-p install path] [-N name_prefix] [-b bin_path] [-c config_path] [-o other]" ;;
    esac
  done
  if $DEBUG ;then
    echo -e "Parameter name: $name"
    echo -e "Parameter version: $version"
    echo -e "Parameter path: $path"
    echo -e "Parameter name_prefix: $name_prefix"
    echo -e "Parameter bin_path: $bin_path"
    echo -e "Parameter config_path: $config_path"
    echo -e "Parameter other_param: $other_param"
  fi
  #exit
  all_name_file=${deb_path}/ts${install_config_path}/ts/all_name
  deb_target_path=$deb_path/ts$install_lib_path/deb
  rm $all_name_file
  rm $deb_target_path/*.deb
  IFS=';' read -r -a array <<< "$other_param"
  for ((i=0; i<${#array[@]}; i+=2)); do
    if $DEBUG ;then
      echo -e "Debug:\t\t${array[i]} ${array[i+1]}"
    fi
    local name=${array[i]}
    local version=${array[i+1]}

    cp $deb_package_path/${name}*.deb $deb_target_path
    if [[ "$name" == lib* ]]; then
      echo "libs_name+=(${name})" >> "${all_name_file}"
    else
      echo "programs_name+=(${name})" >> "${all_name_file}"
    fi
  done
  echo "task_name+=(mobiledevice taskpush)" >> "${all_name_file}"
  extra_lib=(libiec60870 libgpiod2 libmodbus5 libmosquitto1 libwget0 libxml2)
  for cell in ${extra_lib[@]}; do
    cp $deb_package_path/${cell}*.deb $deb_target_path
  done
  version=1.0.2
  dpkg-deb -b -Zgzip $deb_path/ts $deb_package_path/ts_${version}_$architecture.deb
}
